/*
 * IndentPrintPrep.java
 * 
 * Copyright (c) 2012 Supin P Surendran <supinps@gmail.com>.
 * 
 * This file is part of InventoryMan.
 * 
 * InventoryMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * InventoryMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with InventoryMan.  If not, see <http ://www.gnu.org/licenses/>.
 */

package InventoryMan;

import com.itextpdf.text.*;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Supin P Surendran <supinps@gmail.com>
 */
public class IndentPrintPrep {

    public IndentPrintPrep() {
        initComponents();
    }
    /**
     * Initializing Components required for the preparation of
     * Document
     */
    private void initComponents() {
        try {
            FILE = System.getProperty("java.io.tmpdir")+System.getProperty("file.separator")+"indent.pdf";

            big = new Font(FontFamily.TIMES_ROMAN, 20);
            normal = new Font(FontFamily.TIMES_ROMAN, 12);
            normalBold = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD | Font.UNDERLINE);
            small = new Font(FontFamily.TIMES_ROMAN, 10);
            newLine=new Paragraph(new Phrase("\n"));
            
            document = new Document();
            docWriter = PdfWriter.getInstance(document, new FileOutputStream(FILE));
            Rectangle rc=new Rectangle(36, 54, 559, 788);
            rc.setBorder(Rectangle.LEFT|Rectangle.RIGHT|Rectangle.TOP|Rectangle.BOTTOM);
            docWriter.setBoxSize("art",rc);
            footer=new Footer();
            docWriter.setPageEvent(footer);
            document.open();
            document.setPageSize(PageSize.A4);
            document.setMargins(20, 20, 20, 20);

            addHeader();
            addTitle();
            Csl=new PdfPCell(new Phrase("Sl No.",normal));
            Cname=new PdfPCell(new Phrase("Description",normal));
            Carea=new PdfPCell(new Phrase("Area",normal));
            Cqty=new PdfPCell(new Phrase("Quantity",normal));
            Crate=new PdfPCell(new Phrase("Rate",normal));
            Camt=new PdfPCell(new Phrase("Amount",normal));
            Cremarks=new PdfPCell(new Phrase("Remarks",normal));
            items = new PdfPTable(7);
            items.setWidthPercentage(100);
            try {
                items.setWidths(new float[]{14.53f, 34.01f, 24.29f, 17.90f, 22.23f, 20.64f, 36.53f});
            } catch (DocumentException ex) {
                Logger.getLogger(IndentPrintPrep.class.getName()).log(Level.SEVERE, null, ex);
            }
            addItems();
            items.setHeaderRows(1);
        }
        catch (Exception ex) {
            javax.swing.JOptionPane.showMessageDialog(null, "Error Occured !");
            ex.printStackTrace();
        }
    }
    /**
     * To create the Header of the Document
     */
    private void addHeader() {
        header=new PdfPTable(2);
            header.setWidthPercentage(100);
            tin=new PdfPCell(new Phrase("TIN : 32040686092",small));
            cst=new PdfPCell(new Phrase("C.S.T : 04 06 C 115086",small));
            tin.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_LEFT);
            tin.setBorder(0);
            cst.setBorder(0);
            cst.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_RIGHT);
            header.addCell(tin);
            header.addCell(cst);
        try {
            document.add(header);
        } catch (DocumentException ex) {
            Logger.getLogger(IndentPrintPrep.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /**
     * To create the Title of the Document
     */
    private void addTitle() {
        container=new Paragraph();
        title=new Paragraph("Kerala State Coir Corporation Ltd",big);
        title.setAlignment(Paragraph.ALIGN_CENTER);
        address=new Paragraph("P.B No 191,  Vazhichery Ward, Alappuzha\nPin Code : 688 001",normal);
        address.setAlignment(Paragraph.ALIGN_CENTER);
        INDENT=new Paragraph("Indent",normalBold);
        INDENT.setAlignment(Paragraph.ALIGN_CENTER);

        container.add(title);
        container.add(address);
        container.add(INDENT);
        try {
            document.add(container);
            document.add(newLine);
        } catch (DocumentException ex) {
            Logger.getLogger(IndentPrintPrep.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /**
     * To add the Indent Entries to the Document
     * @param customer customer name
     * @param oNo  order number
     * @param oDate order date
     * @param iNo indent number
     * @param iDate indent date
     * @param dDate due date
     */
    public void addEntry(String customer,String oNo,String oDate,String iNo,String iDate,String dDate) {
        entry=new PdfPTable(3);
        entry.setWidthPercentage(100);
        try {
            entry.setWidths(new float[]{123.81f, 28.05f, 19.90f});
        } catch (DocumentException ex) {
            Logger.getLogger(IndentPrintPrep.class.getName()).log(Level.SEVERE, null, ex);
        }

        first=new PdfPCell();
        first.setBorder(PdfPCell.NO_BORDER);
        second=new PdfPCell();
        second.setBorder(PdfPCell.NO_BORDER);
        third=new PdfPCell();
        third.setBorder(PdfPCell.NO_BORDER);

        first.setPhrase(new Phrase("",normal));
        second.setPhrase(new Phrase("Order No",normal));
        third.setPhrase(new Phrase(oNo,normal));
        entry.addCell(first);
        entry.addCell(second);
        entry.addCell(third);

        first.setPhrase(new Phrase("",normal));
        second.setPhrase(new Phrase("Order Date",normal));
        third.setPhrase(new Phrase(oDate,normal));
        entry.addCell(first);
        entry.addCell(second);
        entry.addCell(third);

        first.setPhrase(new Phrase("Customer : "+customer,normal));
        second.setPhrase(new Phrase("Due Date",normal));
        third.setPhrase(new Phrase(dDate,normal));
        entry.addCell(first);
        entry.addCell(second);
        entry.addCell(third);

        first.setPhrase(new Phrase("Indent No : "+iNo,normal));
        second.setPhrase(new Phrase("Indent Date",normal));
        third.setPhrase(new Phrase(iDate,normal));
        entry.addCell(first);
        entry.addCell(second);
        entry.addCell(third);
        try {
            document.add(entry);
            document.add(newLine);
        } catch (DocumentException ex) {
            Logger.getLogger(IndentPrintPrep.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /**
     * Initializes items table
     */
    private void addItems() {
        items.addCell(Csl);
        items.addCell(Cname);
        items.addCell(Carea);
        items.addCell(Cqty);
        items.addCell(Crate);
        items.addCell(Camt);
        items.addCell(Cremarks);
    }
    /**
     * To add Indent Items to the Document
     * @param sl Serial No of items
     * @param name Name of the item
     * @param area Area of the item
     * @param qty Quantity of the item
     * @param rate Rate of an item
     * @param amt Amount of Item
     * @param rmks Remarks about the item, if any
     */
    public void newItem(String sl,String name,String area,String qty,String rate,String amt,String rmks) {
        Csl.setPhrase(new Phrase(sl));
        Cname.setPhrase(new Phrase(name));
        Carea.setPhrase(new Phrase(area));
        Cqty.setPhrase(new Phrase(qty));
        Crate.setPhrase(new Phrase(rate));
        Camt.setPhrase(new Phrase(amt));
        Cremarks.setPhrase(new Phrase(rmks));
        addItems();
    }
    public void writeItems() {
        try {
            document.add(items);
            document.add(newLine);
            document.add(newLine);
            addFooter();
            document.close();
        } catch (DocumentException ex) {
            Logger.getLogger(IndentPrintPrep.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    private void addFooter() {
         try {
            PdfPTable ver = new PdfPTable(3);
            ver.setWidthPercentage(100);
            PdfPCell prep=new PdfPCell(new Phrase("Prepared By"));
            PdfPCell chk=new PdfPCell(new Phrase("Checked By"));
            PdfPCell hod=new PdfPCell(new Phrase("HOD Sales"));
            prep.setBorder(PdfPCell.NO_BORDER);
            prep.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            chk.setBorder(PdfPCell.NO_BORDER);
            chk.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            hod.setBorder(PdfPCell.NO_BORDER);
            hod.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            ver.addCell(prep);
            ver.addCell(chk);
            ver.addCell(hod);
            document.add(ver);
        } catch (DocumentException ex) {
            
        }
    }
    public void initPrint() {
         PdfViewer pv=new PdfViewer(FILE);
    }
    //Variable Declarations
    String FILE;
    Font big;
    Font normal;
    Font normalBold;
    Font small;
    Document document;
    PdfWriter docWriter;
    Paragraph newLine;
    //Title Section
    Paragraph container;
    PdfPTable header;
    PdfPCell tin,cst;
    Paragraph title;
    Paragraph address;
    Paragraph INDENT;
    //Title Ends
    //Entry Section
    PdfPTable entry;
    PdfPCell first,second,third;
    //Entry Ends
    //Items Section
    PdfPTable items;
    PdfPCell Csl,Cname,Carea,Cqty,Crate,Camt,Cremarks;
    //Items Ends
    PdfViewer viewPdf;
    Footer footer;

    /**
     * This class contains event handler for endofpage event
     * to attach page number as footer to the page
     */
    public class Footer extends PdfPageEventHelper {
        @Override
        public void onEndPage (PdfWriter writer, Document document) {
            Rectangle rect = writer.getBoxSize("art");
            ColumnText.showTextAligned(writer.getDirectContent(),
            Element.ALIGN_CENTER, new Phrase(String.format("page : %d", writer.getCurrentPageNumber())),
            (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
        }
    }
}
